<?php
require_once 'database.php';



/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class login_reg extends database{
    public $result;

            function  __construct() {
                parent::__construct();
            }

            function  __destruct() {

            }

            function validation_reg($new_user_id){
                $query = "SELECT usr_email FROM user WHERE usr_email='$new_user_id'";
                $this->connect();
		$exec = mysql_query($query); //eksekusi

                

                if (mysql_num_rows($exec) > 0) return false;
                else return true;

		mysql_close();
                
            }

            function validation_login($user_id, $pass){
                $query = "SELECT usr_email,password FROM user WHERE usr_email='$user_id' AND password='$pass'";
                $this->connect();
		$exec = mysql_query($query); //eksekusi

                $q = "SELECT status FROM user WHERE usr_email='$user_id'";
                $this->exec_q($q, "status");

                if (mysql_num_rows($exec) > 1 || mysql_num_rows($exec) == 0  || $this->result[0] == 0) return false;
                else return true;

		mysql_close();

            }

            function is_admin($user_id, $pass){
                 $query = "SELECT role FROM user WHERE usr_email='$user_id' AND password='$pass'";
                $this->connect();
		$exec = mysql_query($query); //eksekusi

                if (mysql_result($exec,"role") == 1) return true;
                else return false;
                
            }

            function register($usr_id, $pass, $nama, $bir, $lok, $hob){

                if ($this->validation_reg($usr_id) == true){
                    $query = "INSERT INTO user (usr_email,password,nama_lengkap,birthday,lokasi,hobi)
                                VALUES('$usr_id','$pass', '$nama', '$bir', '$lok', '$hob')";

                    $this->connect();
                    mysql_query($query,$this->conn);
                    mysql_close();
                    return true;
                }
                else return false;
            }

            function change_username($old, $new){
                $query = "UPDATE user SET usr_email='$new' WHERE usr_email='$old'";

		$this->connect();
		mysql_query($query,$this->conn);
		mysql_close();
            }

            function change_password($usr, $new){
                $query = "UPDATE user SET password='$new' WHERE usr_email='$usr'";

		$this->connect();
		mysql_query($query,$this->conn);
		mysql_close();
            }

            function retr_pass($usr_id){

		$query = "Select password from user WHERE usr_email = $usr_id";
		$res = $this->exec_q($query,$col);
		$this->result = $res;
                      //hasil = $this->result[idx]
		return $res;
            }

            function validate_pass($usr_id, $input){
                $res = retr_pass($usr_id);
                if ($input == $res) return true;
                else return false;
            }

            function setOnline($usr_id, $val){
                $query = "UPDATE user SET online=$val WHERE usr_email='$usr_id'";
                
                $this->connect();
                mysql_query($query,$this->conn);
                mysql_close();
            }
}
?>